package com.cloudsigns.factorymanager2.engineering.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPatrolCompletionForTeamView;
import com.cloudsigns.factorymanager2.engineering.IView.IReceiveOrBackPatrolOrderView;
import com.cloudsigns.factorymanager2.engineering.adapter.MediaForPublicationAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolFeedbackBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.PublicationOrderDetailBean;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolCompletionForTeamPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.ReceiveOrBackPatrolOrderPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.ReceiveOrBackPublicationOrderPresenter;
import com.cloudsigns.factorymanager2.event.EngineeringOrderDetailForTeamEvent;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by tony on 2018/5/27.
 */

public class WorkOrderInfomationForPatrolFragment extends Fragment implements View.OnClickListener, IReceiveOrBackPatrolOrderView, IPatrolCompletionForTeamView {
    private TextView edt_project_name, edt_media_number, edt_pubication_cycle_time, edt_dispath_order_person_colon, edt_dispath_order_time_colon;
    private TextView btn_receive_order, btn_back_order;
    private View ll_receive_back_btn;
    private TextView tv_completion_statu;
    private View btn_submit_completion_order;
    private boolean isClick = true, isSubmitCompletion; //是否能提交完工单
    private final String TAG = WorkOrderInfomationForPublicationFragment.class.getSimpleName();
    private LayoutInflater layoutInflater;
    private PatrolOrderBean patrolOrderBean;
    Map<String, List<MediaBean>> mediaBeanMap = new HashMap<>();
    private CommonEnum.STATUS status;
    private String assignReceiveOrderId;
    private ReceiveOrBackPatrolOrderPresenter receiveOrBackPatrolOrderPresenter;
    private PatrolCompletionForTeamPresenter patrolCompletionForTeamPresenter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        receiveOrBackPatrolOrderPresenter = new ReceiveOrBackPatrolOrderPresenter(this);
        patrolCompletionForTeamPresenter = new PatrolCompletionForTeamPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_workorderinfomation_for_patrol, null);

        edt_project_name = view.findViewById(R.id.edt_project_name);
        edt_media_number = view.findViewById(R.id.edt_media_number);
        edt_pubication_cycle_time = view.findViewById(R.id.edt_pubication_cycle_time);
        edt_dispath_order_person_colon = view.findViewById(R.id.edt_dispath_order_person_colon);
        edt_dispath_order_time_colon = view.findViewById(R.id.edt_dispath_order_time_colon);
        tv_completion_statu = view.findViewById(R.id.tv_completion_statu);

        btn_receive_order = view.findViewById(R.id.btn_receive_order);
        btn_back_order = view.findViewById(R.id.btn_back_order);
        ll_receive_back_btn = view.findViewById(R.id.ll_receive_back_btn);

        btn_submit_completion_order = view.findViewById(R.id.btn_submit_completion_order);


        btn_receive_order.setOnClickListener(this);
        btn_back_order.setOnClickListener(this);
        btn_submit_completion_order.setOnClickListener(this);

        layoutInflater = getActivity().getLayoutInflater();


        return view;
    }

    public void initData(PatrolOrderBean patrolOrderBean) {
        if (null != patrolOrderBean) {
            this.patrolOrderBean = patrolOrderBean;
            edt_project_name.setText(patrolOrderBean.getRegionName());
            List<MediaBean> mediaBeanList = patrolOrderBean.getMediaList();
            if (null != mediaBeanList)
                edt_media_number.setText(mediaBeanList.size() + "");

            if (null != mediaBeanList && mediaBeanList.size() > 0) {
                MediaBean mediaBean = mediaBeanList.get(0);
                assignReceiveOrderId = mediaBean.getAssignReceiveOrderId();
            }


            edt_pubication_cycle_time.setText(DateUtil.getYyyy_Mm_Dd(patrolOrderBean.getBeginDate()) + "/" + DateUtil.getYyyy_Mm_Dd(patrolOrderBean.getFinishDate()));
            edt_dispath_order_person_colon.setText(patrolOrderBean.getReceiveAccountId());
            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(patrolOrderBean.getSendOrderTime()));

            List<PatrolFeedbackBean> patrolFeedbackBeanList = patrolOrderBean.getPatrolFeedbackList();

            if (null != patrolFeedbackBeanList && patrolFeedbackBeanList.size() > 0) {
                btnClickable_submit(true);
            } else {
                btnClickable_submit(false);
            }

            CommonEnum.CompleteStatus completeStatus = patrolOrderBean.getCompleteStatus();
            if (null != completeStatus) {
                if (completeStatus == CommonEnum.CompleteStatus.WORKING) {
                    tv_completion_statu.setText("");
                } else if (completeStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    tv_completion_statu.setText("[等待确认]");
                    tv_completion_statu.setTextColor(getResources().getColor(R.color.rgb_FCD505));
                    btnClickable_submit(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    tv_completion_statu.setText("[等待确认]");
                    tv_completion_statu.setTextColor(getResources().getColor(R.color.rgb_FCD505));
                    btnClickable_submit(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                    tv_completion_statu.setText("[竣工]");
                    tv_completion_statu.setTextColor(getResources().getColor(R.color.rgb_74BF73));
                    btnClickable_submit(false);
                }
            }

            CommonEnum.EngineeringOrderStatus status = patrolOrderBean.getStatus();
            if (null != status) {
                if (status == CommonEnum.EngineeringOrderStatus.CREATED) {
                    btnClickable(true);
                } else if (status == CommonEnum.EngineeringOrderStatus.SEND) {
                    btnClickable(true);
                } else if (status == CommonEnum.EngineeringOrderStatus.RECEIVED) {
                    btnClickable(false);
                } else if (status == CommonEnum.EngineeringOrderStatus.RETURNED) {
                    btnClickable(false);
                } else if (status == CommonEnum.EngineeringOrderStatus.COMPLETED) {
                    btnClickable(false);
                }
            }

        }
    }

    private void btnClickable_submit(boolean bool) {
        isSubmitCompletion = bool;
        if (bool) {
            btn_submit_completion_order.setBackgroundResource(R.mipmap.btn_shor_bg_blue);
        } else {
            btn_submit_completion_order.setBackgroundResource(R.mipmap.btn_bg_short_gray);
        }
    }

    private void btnClickable(boolean bool) {
        isClick = bool;
        if (bool) {
            btn_receive_order.setBackgroundResource(R.color.rgb_00B050);
            btn_back_order.setBackgroundResource(R.color.rgb_C00000);
        } else {
            btn_receive_order.setBackgroundResource(R.color.rgb_D9D9D9);
            btn_back_order.setBackgroundResource(R.color.rgb_D9D9D9);
        }
    }


    private void receiveOrder() {
        status = CommonEnum.STATUS.RECEIVED;
        btnClickable(false);
        receiveOrBackPatrolOrderPresenter.receiveOrBackPatrolOrder();
    }

    private void backOrder() {
        status = CommonEnum.STATUS.RETURNED;
        btnClickable(false);
        receiveOrBackPatrolOrderPresenter.receiveOrBackPatrolOrder();
    }

    private void patrolCompletion() {
        patrolCompletionForTeamPresenter.patrolCompletionForTeam();
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.btn_receive_order:
                if (isClick)
                    receiveOrder();
                break;
            case R.id.btn_back_order:
                if (isClick)
                    backOrder();
                break;
            case R.id.btn_submit_completion_order:
                if (isSubmitCompletion)
                    patrolCompletion();
                break;


        }
    }


    @Override
    public String getAssignReceiveOrderId() {
        return assignReceiveOrderId;
    }

    @Override
    public String getOrderId() {
        if (null != patrolOrderBean)
            return patrolOrderBean.getId();
        return "";
    }

    @Override
    public CommonEnum.STATUS getStatus() {
        return status;
    }

    @Override
    public void receiveOrBackOrderSuccessed() {
        if (null != status) {
            if (status == CommonEnum.STATUS.RECEIVED)
                Toast.makeText(getActivity(), "接收成功！", Toast.LENGTH_SHORT).show();
            else if (status == CommonEnum.STATUS.RETURNED)
                Toast.makeText(getActivity(), "退回成功！", Toast.LENGTH_SHORT).show();
        }
        EventBus.getDefault().post(new EngineeringOrderDetailForTeamEvent());
        btnClickable(false);
    }

    @Override
    public void receiveOrBackOrderFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
        btnClickable(true);
    }

    @Override
    public void patrolCompletionForTeamSuccess() {
        Toast.makeText(getActivity(), "提交完结成功", Toast.LENGTH_SHORT).show();
        btnClickable(false);
    }

    @Override
    public void patrolCompletionForTeamFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

}
